package com.yun.workqueue;

import com.rabbitmq.client.*;
import com.yun.utils.ConnectionUtil;
import org.junit.Test;

import java.io.IOException;

public class ConsumeB {

    public static void main(String[] arg) throws IOException {

        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("work", false, false, false, null);
        //每次只给消费者发送一条消息
        channel.basicQos(1);

        /**
         * 参数1：work-队列名称
         * 参数2：消息是否自动确认
         */
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("msg 2 = " + new String(body));
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //消息的标记，是否开启多条消息确认机制
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });


    }
}
